﻿using IronPython.Runtime;
using NPOI.XSSF.UserModel.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils
{
    public static class LFile
    {
        /// <summary>
        /// if file1's LastWriteTime is newer than file2's , return true.
        /// </summary>
        /// <returns></returns>
        public static bool IsNewerThanAnotherFile(this string fileFullPath1, string fileFullPath2)
        {
            try
            {
                if (System.IO.File.Exists(fileFullPath1) != true) return false;
                if (System.IO.File.Exists(fileFullPath2) != true) return false;

                FileInfo fi1 = new FileInfo(fileFullPath1);
                FileInfo fi2 = new FileInfo(fileFullPath2);
                return fi1.LastWriteTime.CompareTo(fi2.LastWriteTime) > 0;
            }
            catch
            {
                return false;
            }
        }

        public static bool FileExist(this string fileFullPath)
        {
            return System.IO.File.Exists(fileFullPath);
        }

        public static MemoryStream ReadImageToMemoryStream(string imagePath)
        {
            // 创建一个Bitmap对象来读取图像文件
            using (Bitmap bitmap = new Bitmap(imagePath))
            {
                // 创建一个MemoryStream用于存储图像数据
                MemoryStream memoryStream = new MemoryStream();

                // 将Bitmap保存到MemoryStream中
                bitmap.Save(memoryStream, bitmap.RawFormat);

                // 重设MemoryStream的位置到文件开头
                memoryStream.Position = 0;

                // 返回包含图像数据的MemoryStream
                return memoryStream;
            }
        }

        public static Bitmap AddBorder(this Bitmap bitmap, float w)
        {
            // float w = (float)(bitmap.Width * 0.2);//边框的宽度，可取任意值
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                // g.CopyFromScreen(0, 0, 0, 0, Screen.AllScreens[0].Bounds.Size);//抓取背景图
                if (w < 0)
                {
                    using (Pen pen = new Pen(Color.Transparent, -w))
                    {
                        //g.DrawRectangle(pen, new Rectangle(0, 0, Screen.AllScreens[0].Bounds.Size.Width, Screen.AllScreens[0].Bounds.Size.Height));//给当前桌面画白框
                        g.DrawRectangle(pen, new Rectangle(0, 0, Math.Abs(bitmap.Width), Math.Abs(bitmap.Height)));//加边框
                                                                                                                   //g.DrawEllipse(pen, new Rectangle(0, 0, Math.Abs(bitmap.Width), Math.Abs(bitmap.Height)));//加椭圆边框
                        g.Dispose();
                        // pic.BackgroundImage = bit;
                    }
                }
                else
                {
                    using (Pen pen = new Pen(Color.Transparent, w))
                    {
                        //g.DrawRectangle(pen, new Rectangle(0, 0, Screen.AllScreens[0].Bounds.Size.Width, Screen.AllScreens[0].Bounds.Size.Height));//给当前桌面画白框
                        g.DrawRectangle(pen, new Rectangle(0, 0, Math.Abs(bitmap.Width), Math.Abs(bitmap.Height)));//加边框
                                                                                                                   //g.DrawEllipse(pen, new Rectangle(0, 0, Math.Abs(bitmap.Width), Math.Abs(bitmap.Height)));//加椭圆边框
                        g.Dispose();
                        // pic.BackgroundImage = bit;
                    }
                }
            }
            return bitmap;
        }

        public static bool IsValidFilePath(string filePath, bool mustExist)
        {
            try
            {
                string fullPath = Path.GetFullPath(filePath);
                // 可选：检查fullPath是否指向文件而不是目录

                if (mustExist)
                {
                    return File.Exists(fullPath) || Directory.Exists(fullPath);
                }
                else return true;
            }
            catch (ArgumentException)
            {
                // 路径无效
                return false;
            }
            catch (PathTooLongException)
            {
                // 在Windows系统上，路径太长也会引发此异常
                return false;
            }
        }

        public static string ReadUTF8TextFile(string filePath)
        {
            return System.IO.File.ReadAllText(filePath);
        }

        public static PythonList ReadUTF8Lines(string filePath)
        {
            var lines = System.IO.File.ReadAllLines(filePath);

            PythonList result = new PythonList();
            foreach (var item in lines)
            {
                result.Add(item);
            }

            return result;
        }
    }
}
